<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class products extends Base
{
    public function products()
    {
        if(!empty(input("param.c_id"))){
            $c_id=input("param.c_id");
        }else{
            return json(['code' => 501, 'msg' => '分类id呢']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        $map = [
            ['state', '=', 1]
        ];
        $res=Db::name('product')->where($map)->where('c_id',$c_id)->field('id,title,img')->order('sort desc')->limit(10*($page-1),10)->select();
        if($res){
            foreach ($res as $key => $value) {
                $res[$key]['price']=Db::name('spec_combination')->where('p_id',$res[$key]['id'])->where('state',1)->order('id asc')->value('money');
            }
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
    }
    public function productAlls()
    {
        $map = [
            ['state', '=', 1]
        ];
        if(!empty(input("param.c_id"))){
            $f_id=Db::name('cates')->where('id',input("param.c_id"))->find();
            if($f_id['f_id']>0){
                array_push($map,['c_id','=',input("param.c_id")]);
            }else{
                $cIds=array();
                $cateIds=Db::name('cates')->where('f_id',input("param.c_id"))->where('state',1)->select();
                if(count($cateIds)>0){
                    foreach ($cateIds as $key => $value) {
                        array_push($cIds,$cateIds[$key]['id']);
                    }
                    array_push($map,['c_id','in',$cIds]);
                }
            }
        }else{
            return json(['code' => 501, 'msg' => '分类id呢']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        $res=Db::name('product')->where($map)->field('id,title,img')->order('sort desc')->limit(10*($page-1),10)->select();
        if($res){
            foreach ($res as $key => $value) {
                $res[$key]['price']=Db::name('spec_combination')->where('p_id',$res[$key]['id'])->where('state',1)->order('id asc')->value('money');
            }
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
    }
    //产品搜索
    public function productSearch(){
        if(!empty(input("param.keyword"))){
            $keyword=input('param.keyword');
        }else{
            return json(['code' => 501, 'msg' => '搜索关键词呢']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        $map = [
            ['state', '=', 1]
        ];
        $orderby = 'sort desc';
        $products=Db::name('product')->where($map)->where('title','like',"%{$keyword}%")->field('id,title,img')->order($orderby)->limit(10*($page-1),10)->select();
        if($products){
            foreach ($products as $key => $value) {
                $products[$key]['price']=Db::name('spec_combination')->where('p_id',$products[$key]['id'])->where('state',1)->order('id asc')->value('money');
            }
            return json(['code' => 200, 'msg' => '成功！','data'=>$products]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
    }
}